package com.csii.user.rest;

import cn.hutool.http.Header;
import com.csii.core.api.Result;
import com.csii.core.constant.CommonConstant;
import com.csii.core.system.vo.ComboModel;
import com.csii.core.system.vo.SysPermissionDataRuleModel;
import com.csii.core.system.vo.SysUserCacheInfo;
import com.csii.user.entity.SysDepart;
import com.csii.user.entity.SysRole;
import com.csii.user.entity.SysUser;
import com.csii.user.service.ISysUserBaseService;
import constant.RedirectConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description TODO
 * @Date 2020/11/4 13:23
 * @Created by Ian
 * @Contact: 19107614@qq.com
 */
@Service
public class SysUserServiceRest implements ISysUserBaseService {
    @Value("${user.service.url}")
    private String userServicePrefix="";
    @Autowired
    RestTemplate restTemplate;

    @Override
    public SysUser getUserByName(String userName) {
        return restTemplate.getForObject(userServicePrefix+"/sys/user/getUserByName?userName={1}",SysUser.class,userName);
    }

    @Override
    public List<SysUser> getUserAll(List<String> userNameList) {
        return restTemplate.exchange(userServicePrefix + "/sys/user/getUserAll", HttpMethod.GET, null, new ParameterizedTypeReference<List<SysUser>>() {},userNameList).getBody();
    }

    @Override
    public List<SysUser> selectAllList() {
        return restTemplate.exchange(userServicePrefix + "/sys/user/selectAllList", HttpMethod.GET, null, new ParameterizedTypeReference<List<SysUser>>() {}).getBody();
    }

    @Override
    public Result<SysUser> queryById(String id) {
        return restTemplate.exchange(userServicePrefix+"/sys/user/queryById?id={1}",HttpMethod.GET, null,new ParameterizedTypeReference<Result<SysUser>>() {},id).getBody();
    }

    @Override
    public Result<List<SysDepart>> listUserDepartList(String userId) {
        return restTemplate.exchange(userServicePrefix+"/listUserDepartList?userId={1}",HttpMethod.GET, null,new ParameterizedTypeReference<Result<List<SysDepart>>>() {},userId).getBody();
    }
    //    String loginByPassword(@RequestParam("grant_type") String grantType, @RequestParam("username") String userName, @RequestParam("password") String passWord, @RequestHeader("Authorization") String authorization );
    @Override
    public Map<String, String> loginByPassword(String grant_type, String username, String password, String authorization) {
        HttpHeaders header=new HttpHeaders();
        header.add(Header.AUTHORIZATION.getValue(), authorization);
        HttpEntity httpEntity=new HttpEntity(header);
        Map<String,Object> map=new HashMap<>();
        map.put("grant_type",grant_type);
        map.put("username",username);
        map.put("password",password);
//        Map<String,Object> rMap=restTemplate.postForObject(userServicePrefix + "/oauth/token?grant_type={grant_type}&username={username}&password={password}", httpEntity, Map.class, map);
//        Map<String,String> sMap=new HashMap<>();
//        rMap.forEach((k,v)->{
//            sMap.put(k,v==null?"":v.toString());
//        });
        return restTemplate.postForObject(userServicePrefix+"/oauth/token?grant_type={grant_type}&username={username}&password={password}",httpEntity,Map.class,map);
    }
    //    Map<String,String> loginByCode(@RequestParam("grant_type") String grantType, @RequestParam("code") String code, @RequestParam("redirect_uri")String  redirectUri, @RequestHeader("Authorization") String authorization );
    @Override
    public Map<String, String> loginByCode(String grant_type, String code, String redirect_uri, String authorization) {
        HttpHeaders header=new HttpHeaders();
        header.add(Header.AUTHORIZATION.getValue(), authorization);
        HttpEntity httpEntity=new HttpEntity(header);
        Map<String,Object> map=new HashMap<>();
        map.put("grant_type",grant_type);
        map.put("redirect_uri",redirect_uri);
        map.put("code",code);
        Map<String,Object> rMap=restTemplate.postForObject(userServicePrefix+"/oauth/token?grant_type={grant_type}&redirect_uri={redirect_uri}&code={code}",httpEntity,Map.class,map);
        Map<String,String> sMap=new HashMap<>();
        rMap.forEach((k,v)->{
            sMap.put(k,v==null?"":v.toString());
        });
        return sMap;
    }

    //    String refreshToken(@RequestParam("grant_type") String grantType, @RequestParam("refresh_token") String refreshToken,@RequestHeader("Authorization") String authorization );
    @Override
    public String refreshToken(String grant_type, String refresh_token, String authorization) {
        HttpHeaders header=new HttpHeaders();
        header.add(Header.AUTHORIZATION.getValue(), authorization);
        HttpEntity httpEntity=new HttpEntity(header);
        Map<String,Object> map=new HashMap<>();
        map.put("grant_type",grant_type);
        map.put("refresh_token",refresh_token);
        return restTemplate.postForObject(userServicePrefix+RedirectConstant.REFRESH_TOKEN,httpEntity,String.class,map);
    }
    //String checkToken(@RequestParam("token") String token,@RequestHeader("Authorization") String authorization);
    @Override
    public String checkToken(String token, String authorization) {
        HttpHeaders header=new HttpHeaders();
        header.add(Header.AUTHORIZATION.getValue(), authorization);
        HttpEntity httpEntity=new HttpEntity(header);
        Map<String,Object> map=new HashMap<>();
        map.put("token",token);
        return restTemplate.postForObject(userServicePrefix+ RedirectConstant.CHECK_TOKEN,httpEntity,String.class,map);
    }

    @Override
    public Result<List<SysPermissionDataRuleModel>> listDataRuleByUrl(String requestPath, String requestMethod, String component) {
        return restTemplate.exchange(userServicePrefix + "/sys/permission/sys/listDataRuleByUrl?requestPath={1}&requestMethod={2}&component={3}", HttpMethod.POST, null, new ParameterizedTypeReference<Result<List<SysPermissionDataRuleModel>>>() {},requestPath,requestMethod,component).getBody();
    }

    @Override
    public Result<SysUserCacheInfo> getCacheUser(String username) {
        return restTemplate.exchange(userServicePrefix + "/sys/user/getCacheUser?username={1}", HttpMethod.GET, null, new ParameterizedTypeReference<Result<SysUserCacheInfo>>() {},username).getBody();
    }

    @Override
    public List<ComboModel> queryAllUser() {
        return restTemplate.exchange(userServicePrefix + "/sys/user/queryAllUser", HttpMethod.GET, null, new ParameterizedTypeReference<List<ComboModel>>() {}).getBody();
    }

    @Override
    public List<SysUser> getUsersByName(String name) {
        return restTemplate.exchange(userServicePrefix + "/sys/user/getUsersByName/"+name, HttpMethod.GET, null, new ParameterizedTypeReference<List<SysUser>>() {}).getBody();
    }

    @Override
    public List<String> getDepartIdsByUsername(String userName) {
        return restTemplate.exchange(userServicePrefix + "/sys/sysDepart/getDepartIdsByUsername/"+userName, HttpMethod.GET, null, new ParameterizedTypeReference<List<String>>() {}).getBody();
    }

    @Override
    public List<String> getDepartNamesByUsername(String userName) {
        return restTemplate.exchange(userServicePrefix + "/sys/sysDepart/getDepartNamesByUsername/"+userName, HttpMethod.GET, null, new ParameterizedTypeReference<List<String>>() {}).getBody();
    }

    @Override
    public Result<List<SysDepart>> getDepartmentListAndUserIds() {
        return restTemplate.exchange(userServicePrefix + "/sys/sysDepart/getDepartmentListAndUserIds", HttpMethod.GET, null, new ParameterizedTypeReference<Result<List<SysDepart>>>() {}).getBody();
    }

    @Override
    public Result<SysUser> getUsersAndDepartId(String userId) {
        return restTemplate.exchange(userServicePrefix + "/sys/user/getUsersAndDepartId?userId={1}", HttpMethod.GET, null, new ParameterizedTypeReference<Result<SysUser>>() {},userId).getBody();
    }

    @Override
    public Result<List<SysUser>> getUserListByName(String name) {
        return restTemplate.exchange(userServicePrefix + "/sys/sysDepart/getUserListByName?name={1}", HttpMethod.GET, null, new ParameterizedTypeReference<Result<List<SysUser>>>() {}, name).getBody();
    }

    @Override
    public Result<List<SysDepart>> queryDeptLikeDeptName(String deptName) {
        return restTemplate.exchange(userServicePrefix+"/sys/sysDepart/queryDeptLikeDeptName?deptName={1}",HttpMethod.GET, null,new ParameterizedTypeReference<Result<List<SysDepart>>>() {},deptName).getBody();

    }

    @Override
    public Result<List<String>> queryDeptCodeByUserId(String userId) {
        return restTemplate.exchange(userServicePrefix+"/sys/sysDepart/queryDeptCodeByUserId?userId={1}",HttpMethod.GET, null,new ParameterizedTypeReference<Result<List<String>>>() {},userId).getBody();

    }

    @Override
    public Result<List<SysDepart>> queryDeptByDeptCodes(List<String> deptCodes) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<List<String>> entity = new HttpEntity<List<String>>(deptCodes, headers);
        return restTemplate.exchange(userServicePrefix + "/sys/sysDepart/queryDeptByDeptCodes", HttpMethod.POST, entity, new ParameterizedTypeReference<Result<List<SysDepart>>>() {}).getBody();
    }

    @Override
    public Result<List<String>> queryRoleCodeByUserId(String userId) {
        return restTemplate.exchange(userServicePrefix+"/sys/role/queryRoleCodeByUserId?userId={1}",HttpMethod.GET, null,new ParameterizedTypeReference<Result<List<String>>>() {},userId).getBody();
    }

    @Override
    public Result<List<SysRole>> queryRoleLikeRoleName(String roleName) {
        return restTemplate.exchange(userServicePrefix+"/sys/role/queryRoleCodeByUserId?roleName={1}",HttpMethod.GET, null,new ParameterizedTypeReference<Result<List<SysRole>>>() {},roleName).getBody();
    }

    @Override
    public Result<List<SysRole>> queryRoleByRoleCodes(List<String> roleCodes) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<List<String>> entity = new HttpEntity<List<String>>(roleCodes, headers);
        return restTemplate.exchange(userServicePrefix+"/sys/role/queryRoleByRoleCodes",HttpMethod.POST, entity,new ParameterizedTypeReference<Result<List<SysRole>>>() {}).getBody();
    }

    @Override
    public Result<List<SysUser>> queryUserLikeUsernameOrNickname(String usernameOrNickname) {
        return restTemplate.exchange(userServicePrefix+"/sys/user/queryUserLikeUsernameOrNickname?usernameOrNickname={1}",HttpMethod.GET, null,new ParameterizedTypeReference<Result<List<SysUser>>>() {},usernameOrNickname).getBody();

    }

    @Override
    public Result<List<SysUser>> queryUserByUsernames(List<String> usernames) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<List<String>> entity = new HttpEntity<List<String>>(usernames, headers);
        return restTemplate.exchange(userServicePrefix+"/sys/user/queryUserByUsernames",HttpMethod.POST, entity,new ParameterizedTypeReference<Result<List<SysUser>>>() {}).getBody();
    }

    @Override
    public Result<List<SysUser>> queryUserByRoleCode(String roleCode) {
        return restTemplate.exchange(userServicePrefix+"/sys/user/queryUserByRoleCode?roleCode={1}",HttpMethod.GET, null,new ParameterizedTypeReference<Result<List<SysUser>>>() {},roleCode).getBody();
    }

    @Override
    public Result<List<SysUser>> queryHisLeadershipByUsername(String username) {
        return restTemplate.exchange(userServicePrefix+"/sys/user/queryHisLeadershipByUsername?username={1}",HttpMethod.GET, null,new ParameterizedTypeReference<Result<List<SysUser>>>() {},username).getBody();
    }

}
